package com.apsfc.controller;


import com.apsfc.po.User;
import com.apsfc.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("qiantai")
@SessionAttributes("user")
public class UserController {
    @Autowired
    private UsersService usersService;

    //表单提交过来的路径
    @RequestMapping("UserLoginServlet")  //用户登录
    public String checkLogin(@RequestParam("name")String name,
                             @RequestParam("pwd")String pwd,
                             HttpSession session){
        User user = new User();
        user.setName(name);
        user.setPwd(pwd);
        if(usersService.loginCheck(user) != null){
            session.setAttribute("user",usersService.loginCheck(user));
            System.out.println(user.getName()+"登录成功");
            System.out.println(usersService.loginCheck(user));
//            User loginUser = new User();
//            loginUser.setLoginStatus(true);

            return "redirect:index.do";
        }
        else{
            session.setAttribute("error","账号或密码错误");
            System.out.println(user.getName()+"账号或密码错误");
            return "qiantai/login";
        }
    }



    //注销方法
    @RequestMapping("outLogin")
    public String outLogin(HttpSession session){
        //通过session.invalidata()方法来注销当前的session
        System.out.println("！！！用户注销成功！！！");
        session.invalidate();
        return "qiantai/login";
    }

    @RequestMapping("RegServlet")       //用户注册
    public String RegServlet(@RequestParam("name") String name,
                             @RequestParam("pwd") String pwd,
                             String realname,int age,
                             String card,String address,
                             String phone,String email,String code){
        //性别怎么读取?
        User user = new User();
        user.setName(name);
        user.setPwd(pwd);
        user.setRealname(realname);
        user.setAge(age);
        user.setCard(card);
        user.setAddress(address);
        user.setPhone(phone);
        user.setEmail(email);
        user.setCode(code);

        usersService.add(user);
        System.out.println(user);

        return "qiantai/login";
    }

    @RequestMapping("UserModifyServlet")       //用户修改
    public String UserModifyServlet(HttpSession session,
                                    String pwd,
                                    String realname, int age,
                                    String card, String address,
                                    String phone, String email, String code){

        System.out.println(session);
        User user = (User)session.getAttribute("user");
        System.out.println("修改前的用户"+user);
        user.setPwd(pwd);
        user.setRealname(realname);
        user.setAge(age);
        user.setCard(card);
        user.setAddress(address);
        user.setPhone(phone);
        user.setEmail(email);
        user.setCode(code);

        usersService.update(user);

        System.out.println("修改后的用户"+user);

        return "redirect:index.do";
    }

}
